{extend name="admin@index_layout"/}
{block name="main"}
<style>
    .layui-bg-color1 {
        background-color: #6699cc !important;
    }

    .layui-bg-color2 {
        background-color: #009688 !important;
    }

    .layui-bg-color3 {
        background-color: #5FB878 !important;
    }

    .layui-bg-color4 {
        background-color: #f3a406 !important;
    }

    .layui-bg-color5 {
        background-color: #d6502b !important;
    }

    .layui-bg-color6 {
        background-color: #33cc66 !important;
    }

    .layui-bg-color7 {
        background-color: #006600 !important;
    }

    /* 图片放大弹窗样式 */
    #imageModal {
        display: none;
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgba(0, 0, 0, 0.9);
    }

    #modalImage {
        margin: 1% auto;
        display: block;
        max-width: 90%;
        max-height: 90%;
        width: auto;
        height: auto;
    }
</style>
<div class="layui-card">
    {empty name="$no_title"}
    <div class="layui-card-header">列表</div>
    {/empty}

    <div class="layui-card-body">
        {if condition="!empty($text)"}
        <blockquote class="layui-elem-quote text" style="font-size: 16px;">{$text|raw}</blockquote>
        {/if}
        <!-- 搜索页面 -->
        {if $indexList.searchItem}
        {include file="admin@common/search" /}
        {/if}

        <!-- tab选项卡页面 -->
        {if $indexList.tabNav}
        {include file="admin@common/tabNav" /}
        {/if}

        {notempty name="$extraForm"}
        {include file="admin@common/extra" /}
        {/notempty}

        <!-- 数据表格 -->
        <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>

        <!-- 操作按钮 -->
        {include file="admin@common/actionBtn" /}

        <input type="hidden" id="module" value="{$module|default=''}">
        <input type="hidden" id="model" value="{$model|default=''}">

        <!-- 图片放大弹窗 -->
        <div id="imageModal">
            <img id="modalImage" src="">
        </div>
    </div>
</div>

<!-- 表格模板 -->
{include file="admin@common/template" /}

{/block}
{block name="script"}
<script type="text/javascript">
    function closeAllLayers(is_refresh = false) {
        layer.closeAll();
        if (is_refresh) {
            location.reload();
        }
    }

    function bindEventCloseAllLayers(is_parent = false, params = {}) {
        // 创建自定义事件（兼容IE11+）
        const event = new CustomEvent('layerClosed', {
            detail: params,
            bubbles: true,
            cancelable: true
        });
        // 修改为通过顶层窗口派发事件
        if (is_parent && parent !== window) {
            parent.document.dispatchEvent(event);
        } else {
            document.dispatchEvent(event);
        }

        if (is_parent) {
            parent.layer.closeAll();
        } else {
            layer.closeAll();
        }
    }

    // 新增专用子页面关闭方法
    function closeSubpageLayers(subpageUrl) {
        const event = new CustomEvent('subpageClosed', {
            detail: {
                url: subpageUrl,
                timestamp: Date.now()
            }
        });

        document.dispatchEvent(event); // 使用document级事件保证传播
    }

    function layerOpen(url, title, area = ['66vw', '80vh']) {
        layer.open({
            type: 2,
            title: title,
            shadeClose: true,
            isOutAnim: false,
            maxmin: true,
            area: area,
            content: url
        });
    }

    layui.use(['table', 'laydate', 'element', 'layer'], function () {
        var table = layui.table, $ = layui.$, laydate = layui.laydate, element = layui.element, layer = layui.layer;

        var text = $('.text');

        table.render({
            elem: '#dataTable',
            id: "dataTable",
            toolbar: '#toolbarDemo',
            loading: true,
            url: '{$url}',
            cellMinWidth: 80,
            cols: {$indexList.tableColumn|raw},
            page: '{$limit>0? true : false}',
            done: function(res, curr, count){
                text.html(res.text);
            },
            limit: {$limit|default=20}
    });

        //监听单元格编辑
        table.on('edit(dataTable)', function(obj) {
            var model = $('#model').val();
            var value = obj.value,
                data = obj.data,
                field = obj.field;
            $.post('{:url("admin/Common/fieldEdit")}', { 'id': data.id, 'value': value, 'field': field, 'model': model }, function(data) {
                if (data.code == 1) {
                    layer.msg(data.msg);
                    setTimeout(function () {
                        table.reload('dataTable', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        }, 'data')
                    }, 500)
                } else {
                    layer.msg(data.msg);
                }

            })
        });

        /* 多选删除 */
        $(document).on('click', '#delAll', function () {
            layer.confirm('{:lang("是否删除选中数据？")}', {icon: 3}, function (index) {
                layer.close(index);
                var checkStatus = table.checkStatus('dataTable')
                    , data = checkStatus.data;
                var ids = [];
                $(data).each(function (i, o) {
                    ids.push(o.id);
                });
                if (ids.length <= 0) {
                    layer.msg('请选择要操作的数据', {icon: 2});
                    return false;
                }
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{$delAllUrl}", {ids: ids, model: '{$model}', module: '{$module}'}, function (data) {
                    layer.close(loading);
                    if (data.code === 0) {
                        layer.msg(data.msg, {time: 1000, icon: 1}, function () {
                            table.reload('dataTable', {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                            }, 'data');
                            // $('.icon-shuaxin').trigger('click');
                        });
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        });

        {if condition="!empty($batchLayerBtn)"}
        $(document).on('click', '[class*="batch-"]', function () {
            var $btn = $(this);
            var url = $btn.data('url');
            var title = $btn.data('title');

            var checkStatus = table.checkStatus('dataTable'), data = checkStatus.data;
            var ids = [];
            $(data).each(function (i, o) {
                ids.push(o.id);
            });
            if (ids.length === 0) {
                layer.msg('请选择要操作的数据', {icon: 2});
                return false;
            }
            var idsString = ids.join(',');
            layer.open({
                type: 2,
                title: title,
                shadeClose: true,
                isOutAnim: false,
                maxmin: true,
                area: ['66vw', '80vh'],
                content: url + '?ids=' + idsString,
            });
        });
        {/if}

            {if condition="!empty($confirmBtn)"}
            $(document).on('click', '.table-ajax-confirm', function() {
                var that = $(this),
                    href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');

                var msg = $(this).data('msg');
                layer.confirm(msg, { icon: 3, title: '提示信息' }, function(index) {
                    if (!href) {
                        notice.info('请设置data-href参数');
                        return false;
                    }
                    layer.load();
                    $.ajax({
                        type: 'POST',
                        url: href,
                        success: function (data) {
                            if (data.code == 0) {
                                notice.success(data.msg);
                                layer.closeAll()
                                setTimeout(function () {
                                    table.reload('dataTable', {
                                        page: {
                                            curr: 1 //重新从第 1 页开始
                                        }
                                    }, 'data')
                                    if ($.isFunction(parent.closeAllLayers)) {
                                        parent.closeAllLayers()
                                    } else {
                                        closeAllLayers()
                                    }
                                }, 1500)
                            } else {
                                notice.error(data.msg);
                                layer.closeAll()
                            }
                        }
                    });
                });
                return false;
            });
            {/if}

                {if condition="!empty($selectDataConfirmBtn)"}
                $(document).on('click', '.select-data-confirm', function() {
                    var that = $(this),
                        href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');
                    var formData = $('.extra-form').serialize();

                    var msg = $(this).data('msg');
                    layer.confirm(msg, { icon: 3, title: '提示信息' }, function(index) {
                        if (!href) {
                            notice.info('请设置data-href参数');
                            return false;
                        }
                        var checkStatus = table.checkStatus('dataTable'), data = checkStatus.data;
                        var ids = [];
                        $(data).each(function (i, o) {
                            ids.push(o.id);
                        });
                        if (ids.length === 0) {
                            layer.msg('请选择要操作的数据', {icon: 2});
                            return false;
                        }
                        layer.load();
                        $.ajax({
                            type: 'POST',
                            url: href,
                            data: {ids: ids, formData: formData},
                            success: function (data) {
                                if (data.code == 0) {
                                    notice.success(data.msg);
                                    layer.closeAll()

                                    if  (data && data.data && data.data.close === 1) {
                                        console.log(data.data)
                                        setTimeout(function () {
                                            table.reload('dataTable', {
                                                page: {
                                                    curr: 1 //重新从第 1 页开始
                                                }
                                            }, 'data')
                                            if ($.isFunction(parent.closeAllLayers)) {
                                                parent.closeAllLayers(true)
                                            } else {
                                                closeAllLayers()
                                            }
                                        }, 1500)
                                        return false;
                                    } else {
                                        var is_parent = false;
                                        if (data && data.data && data.data.parent === 1) {
                                            is_parent = true
                                        }
                                        setTimeout(function () {
                                            if (is_parent) {
                                                bindEventCloseAllLayers(true, data.data)
                                            } else {
                                                closeAllLayers(true)
                                            }
                                        }, 1500)
                                    }
                                } else {
                                    notice.error(data.msg);
                                    layer.closeAll()
                                }
                            }
                        });
                    });
                    return false;
                });
                {/if}

                    {if condition="!empty($listConfirmBtn)"}
                    $(document).on('click', '.ajax-confirm', function() {
                        var that = $(this),
                            href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');

                        var msg = $(this).data('msg');
                        var repet = $(this).data('repet');
                        var repetMsg = $(this).data('remsg');
                        layer.confirm(msg, { icon: 3, title: '提示信息' }, function(index) {
                            if (!href) {
                                notice.info('请设置data-href参数');
                                return false;
                            }
                            layer.load();
                            if (repet == 1) {
                                layer.confirm(repetMsg, { icon: 3, title: '提示信息' }, function(index) {
                                    layer.load();
                                    requestUrl(href);
                                });
                            } else {
                                requestUrl(href)
                            }
                        });
                        return false;
                    });
                    function requestUrl(href) {
                        $.ajax({
                            type: 'POST',
                            url: href,
                            success: function (data) {
                                if (data.code == 1 || data.code == 0) {
                                    notice.success(data.msg);
                                    setTimeout(function () {
                                        table.reload('dataTable', 'data')
                                    }, 500)
                                } else {
                                    notice.error(data.msg);
                                }
                                layer.closeAll();
                            }
                        });
                    }
                    {/if}

                        {if condition="!empty($fieldSelfBtn)"}
                        $(document).on('click', '.ajax-confirm', function() {
                            var that = $(this),
                                href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');
                            //如果判断状态为禁用，则直接返回
                            if ($(that).hasClass('layui-btn-disabled')) {
                                return false;
                            }

                            var msg = $(this).data('msg');
                            var repet = $(this).data('repet');
                            var repetMsg = $(this).data('remsg');
                            layer.confirm(msg, { icon: 3, title: '提示信息' }, function(index) {
                                if (!href) {
                                    notice.info('请设置data-href参数');
                                    return false;
                                }
                                layer.load();
                                if (repet == 1) {
                                    layer.confirm(repetMsg, { icon: 3, title: '提示信息' }, function(index) {
                                        requestUrl(href);
                                    });
                                } else {
                                    requestUrl(href)
                                }
                            });
                            return false;
                        });
                        function requestUrl(href) {
                            $.ajax({
                                type: 'POST',
                                url: href,
                                success: function (data) {
                                    if (data.code == 1 || data.code == 0) {
                                        notice.success(data.msg);
                                        setTimeout(function () {
                                            table.reload('dataTable', 'data')
                                        }, 500)
                                    } else {
                                        notice.error(data.msg);
                                    }
                                    layer.closeAll();
                                }
                            });
                        }
                        {/if}

                            /* 搜索 */
                            {if $indexList.searchItem}
                            {include file="admin@common/js/search" /}
                            {/if}

                                /* tab选项卡 */
                                {if $indexList.tabNav}
                                {include file="admin@common/js/tabNav" /}
                                {/if}
                                });

                                $(document).ready(function () {
                                    const modal = document.getElementById('imageModal');
                                    const modalImg = document.getElementById('modalImage');
                                    $('.layui-card-body').on('click', 'img', function () {
                                        const imgSrc = $(this).attr('src');
                                        modalImg.src = imgSrc;
                                        modal.style.display = "block";
                                    });

                                    // 点击弹窗外区域关闭弹窗
                                    modal.onclick = function (event) {
                                        if (event.target === modal) {
                                            modal.style.display = "none";
                                        }
                                    };
                                });
</script>
{/block}
